quartz menu: add a hack for application name
authorRyan Lortie <desrt@desrt.ca>
Mon, 16 Dec 2013 16:05:17 +0000 (11:05 -0500)
committerRyan Lortie <desrt@desrt.ca>
Sat, 18 Jan 2014 03:37:04 +0000 (22:37 -0500)
Add a private hack to allow the insertion of the name of the application
into the label of menu items.

If it appears in the label of any menu item, "%s" will be replaced with
the name of the application.

We will use this for the "Hide myapp", "Quit myapp" and "About myapp"
labels typically found on Mac OS programs.

https://bugzilla.gnome.org/show_bug.cgi?id=720552

gtk/gtkapplication-quartz-menu.c

index 60e13d0286f9613f870951371a68694f6c2e0f35..41fbea6a3ea739fee11a476cf4bec5da0903bc23 100644 (file)
@@ -59,6 +59,7 @@
   GtkMenuTrackerItem *trackerItem;
   gulong trackerItemChangedHandler;
   GCancellable *cancellable;
+  BOOL isSpecial;
 }
 
 - (id)initWithTrackerItem:(GtkMenuTrackerItem *)aTrackerItem;
@@ -167,6 +168,7 @@ icon_loaded (GObject      *object,
 
       trackerItem = g_object_ref (aTrackerItem);
       trackerItemChangedHandler = g_signal_connect (trackerItem, "notify", G_CALLBACK (tracker_item_changed), self);
+      isSpecial = (special != NULL);
 
       [self didChangeLabel];
       [self didChangeIcon];
@@ -199,7 +201,29 @@ icon_loaded (GObject      *object,
 {
   gchar *label = _gtk_toolbar_elide_underscores (gtk_menu_tracker_item_get_label (trackerItem));
 
-  [self setTitle:[NSString stringWithUTF8String:label ? : ""]];
+  NSString *title = [NSString stringWithUTF8String:label ? : ""];
+
+  if (isSpecial)
+    {
+      NSRange range = [title rangeOfString:@"%s"];
+
+      if (range.location != NSNotFound)
+        {
+          NSBundle *bundle = [NSBundle mainBundle];
+          NSString *name = [[bundle localizedInfoDictionary] objectForKey:@"CFBundleName"];
+
+          if (name == nil)
+            name = [[bundle infoDictionary] objectForKey:@"CFBundleName"];
+
+          if (name == nil)
+            name = [[NSProcessInfo processInfo] processName];
+
+          if (name != nil)
+            title = [title stringByReplacingCharactersInRange:range withString:name];
+        }
+    }
+
+  [self setTitle:title];
 
   g_free (label);
 }